const { extractKeywords } = require("../src/download_images.js");
const fs = require("fs");
const path = require("path");

// Check if we have command line arguments
const args = process.argv.slice(2);
if (args.length === 0) {
  console.log("Usage: node test_keywords.js <srt_file>");
  process.exit(1);
}

const srtFile = path.join(__dirname, "../", args[0]);

// Check if file exists
if (!fs.existsSync(srtFile)) {
  console.log(`File not found: ${srtFile}`);
  process.exit(1);
}

// Read and parse SRT file
const srtContent = fs.readFileSync(srtFile, "utf-8");
const subtitles = [];
const blocks = srtContent.split("\n\n").filter(block => block.trim() !== "");

blocks.forEach(block => {
  const lines = block.split("\n").filter(line => line.trim() !== "");
  if (lines.length >= 3) {
    subtitles.push({
      text: lines.slice(2).join("\n"),
    });
  }
});

console.log(`解析到 ${subtitles.length} 条字幕`);

// Extract keywords from first 10 subtitles
const testSubtitles = subtitles.slice(0, 10);
const keywords = testSubtitles.map(sub => ({
  text: sub.text,
  keywords: extractKeywords(sub.text),
}));

console.log("提取的关键词（前10个）:");
console.log(JSON.stringify(keywords, null, 2));

console.log("\n测试完成");
